{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Integers With Even Digit Sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countEven"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计各位数字之和为偶数的整数个数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个正整数 <code>num</code> ，请你统计并返回 <strong>小于或等于</strong> <code>num</code> 且各位数字之和为 <strong>偶数</strong> 的正整数的数目。</p>\n",
    "\n",
    "<p>正整数的 <strong>各位数字之和</strong> 是其所有位上的对应数字相加的结果。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = 4\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\n",
    "只有 2 和 4 满足小于等于 4 且各位数字之和为偶数。    \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = 30\n",
    "<strong>输出：</strong>14\n",
    "<strong>解释：</strong>\n",
    "只有 14 个整数满足小于等于 30 且各位数字之和为偶数，分别是： \n",
    "2、4、6、8、11、13、15、17、19、20、22、24、26 和 28 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-integers-with-even-digit-sum](https://leetcode.cn/problems/count-integers-with-even-digit-sum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-integers-with-even-digit-sum](https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['4', '30']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1,num+1):\n",
    "            res = 0\n",
    "            while i > 0:\n",
    "                res = res + i%10\n",
    "                i //= 10\n",
    "            if res % 2 == 0:ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(1,num+1):\n",
    "            nums=0\n",
    "            while i:\n",
    "                nums+=i%10\n",
    "                i=i//10\n",
    "            if nums%2==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(1,num+1):\n",
    "            s=0\n",
    "            for j in str(i):\n",
    "                s+=int(j)\n",
    "            if s%2==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, n: int) -> int:\n",
    "        return sum(sum(int(w) for w in str(num))%2==0 for num in range(1, n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(1,num+1):\n",
    "            if(i%2==0 and len(str(i))<2):\n",
    "                count+=1\n",
    "            elif(len(str(i))>1):\n",
    "                result=0\n",
    "                arr = list(str(i))\n",
    "                for i in arr:\n",
    "                    result+=int(i)\n",
    "                if(result%2==0):\n",
    "                    count+=1\n",
    "        return count\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        b = 0\n",
    "        for i in range(1, num + 1):\n",
    "            k = [int(n) for n in str(i)]\n",
    "            a = 0\n",
    "            for j in k:\n",
    "                a += j\n",
    "            if a % 2 == 0:\n",
    "                b += 1\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        # res=0\n",
    "        # for i in range(1,num+1):\n",
    "        #     if sum(map(int,str(i)))%2==0:\n",
    "        #         res+=1\n",
    "        # return res\n",
    "        if sum(map(int,str(num)))%2==0:\n",
    "            return num//2\n",
    "        return (num-1)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        return len([x for x in range(1,num+1) if self.cou(x)%2==0])\n",
    "\n",
    "    def cou(self,n):\n",
    "        ans=0\n",
    "        while n>0:\n",
    "            ans+=n%10\n",
    "            n//=10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        y, x = divmod(num, 10)\n",
    "        ans = y * 5\n",
    "        y_sum = 0\n",
    "        while y:\n",
    "            y_sum += y % 10\n",
    "            y //= 10\n",
    "        return ans + ((x + 1) // 2 - 1 if y_sum % 2 else x // 2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        res=[]\n",
    "        for i in range(1,num+1):\n",
    "            s=[int(i) for i in list(str(i))]            \n",
    "            if sum(s)<=num and sum(s)%2==0:\n",
    "                res.append(i)            \n",
    "        return len(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, num + 1):\n",
    "            a = list(str(i))\n",
    "            a = \" \".join(a)\n",
    "            b = map(int, a.split())\n",
    "            if sum(b) % 2 == 0:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(1, num+1):\n",
    "            if sum(map(int, list(str(i)))) % 2 == 0: count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(1,num+1):\n",
    "            if(i%2==0 and len(str(i))<2):\n",
    "                count+=1\n",
    "            elif(len(str(i))>1):\n",
    "                result=0\n",
    "                arr = list(str(i))\n",
    "                for i in arr:\n",
    "                    result+=int(i)\n",
    "                if(result%2==0):\n",
    "                    count+=1\n",
    "        return count\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(1, num + 1):\n",
    "            sum = 0\n",
    "            temp = i\n",
    "            while temp > 0:\n",
    "                sum += temp % 10\n",
    "                temp //= 10\n",
    "            if sum %2 == 0:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        def allsum(n: int) -> bool:\n",
    "            #check all digit sum\n",
    "            return not sum([int(i) for i in str(n)]) % 2\n",
    "        \n",
    "        return sum([allsum(n) for n in range(1, num + 1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        if num % 2 == 1:\n",
    "            return num // 2\n",
    "        res = (num - 1) // 2\n",
    "        n = 0\n",
    "        while num > 0:\n",
    "            n += (num % 10)\n",
    "            num //= 10\n",
    "        return res + 1 if n % 2 == 0 else res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        return num // 2 -((num+1)%2)*((num+(num//10)+(num//100)+(num//1000)) % 2)\n",
    "    \n",
    "           \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        def counts(n):\n",
    "            ans = 0\n",
    "            while n:\n",
    "                ans += n % 10\n",
    "                n //= 10\n",
    "            return ans\n",
    "        \n",
    "        if num < 2:\n",
    "            return 0\n",
    "        return sum(1 for n in range(2,num+1) if counts(n) % 2 == 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        r = 0\n",
    "        for i in range(2, num + 1):\n",
    "            if sum(map(int, str(i))) % 2 == 0:\n",
    "                r += 1\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "\n",
    "        count_max = 0\n",
    "        for i in range(2, num + 1):\n",
    "            count = 0\n",
    "            while i >= 1:\n",
    "            \n",
    "                count += i % 10\n",
    "                i = i // 10\n",
    "\n",
    "            if count % 2 == 0:\n",
    "            \n",
    "                count_max += 1\n",
    "\n",
    "        return count_max\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sum(self,n):\n",
    "        total = 0  \n",
    "        while n != 0:  \n",
    "            digit = n % 10  \n",
    "            total += digit  \n",
    "            n = n // 10  \n",
    "        if total % 2 == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "    def countEven(self, num: int) -> int:\n",
    "        t = 0\n",
    "        for i in range(2,num+1,1):\n",
    "            if self.sum(i):\n",
    "                t += 1\n",
    "        return t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        if sum(map(int, str(num))) % 2:\n",
    "            return (num - 1) // 2\n",
    "        return num // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        totle = 0\n",
    "        for i in range(2, num + 1):\n",
    "            sum = 0\n",
    "            for j in str(i):\n",
    "                sum += int(j)\n",
    "            if sum % 2 == 0:\n",
    "                totle += 1\n",
    "        return totle\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        if sum(int(i) for i in str(num)) %2:\n",
    "            return (num-1)//2\n",
    "        return num//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        return sum(sum(map(int, str(i))) % 2 == 0 for i in range(1, num+1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans=0\n",
    "        for x in range(1,num+1):\n",
    "            s=0\n",
    "            while x!=0:\n",
    "                s+=x%10\n",
    "                x//=10\n",
    "            if s%2==0:\n",
    "                ans+=1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, num + 1):\n",
    "            cnt = 0 \n",
    "            for j in str(i): # 步骤1\n",
    "                cnt += int(j) # 步骤2\n",
    "            if cnt % 2 == 0: ans += 1 # 步骤3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        s = 0\n",
    "        \n",
    "        for i in range(1,num+1):\n",
    "            c = 0\n",
    "            while i > 0:\n",
    "                c += i %10 \n",
    "                i = i // 10\n",
    "            if c % 2 == 0:\n",
    "                s += 1\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        cnt = 0\n",
    "        for i in range(1,num+1):\n",
    "            if i<10 and i%2==0:\n",
    "                cnt +=1\n",
    "            elif 10<=i<100 and (i%10+i//10)%2 ==0:\n",
    "                cnt +=1\n",
    "            elif 100<=i<1000 and (i%10+(i//10)%10+i//100)%2 ==0:\n",
    "                cnt +=1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, num + 1):\n",
    "            if sum(ord(x) - ord('0') for x in str(i)) % 2 == 0:\n",
    "                ans += 1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        def countnum(num):\n",
    "            num = list(str(num))\n",
    "            count = 0\n",
    "            for i in range(len(num)):\n",
    "                count += int(num[i])\n",
    "            return count\n",
    "        ans = 0\n",
    "        for n in range(1,num+1):\n",
    "            if countnum(n)%2==0:ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        res=0\n",
    "        for i in range(1,num+1):\n",
    "            if sum(map(int,str(i)))%2==0:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        result = 0\n",
    "        \n",
    "        for i in range(1, num+1):\n",
    "            string = str(i)\n",
    "            num_list = []\n",
    "            for k in range(len(string)):\n",
    "                num_list.append(int(string[k]))\n",
    "                \n",
    "            calculate = sum(num_list)\n",
    "            if calculate % 2 == 0:\n",
    "                result += 1\n",
    "                \n",
    "        return result\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for x in range(1, num+1):\n",
    "            s = 0\n",
    "            while x:\n",
    "                s += x % 10\n",
    "                x //= 10\n",
    "            ans += s % 2 == 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for x in range(1, num + 1):\n",
    "            s = 0\n",
    "            while x:\n",
    "                s += x % 10\n",
    "                x //= 10\n",
    "            ans += s % 2 == 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        cnt=0\n",
    "        for i in range(1,num+1):\n",
    "            sum=0\n",
    "            for j in str(i):\n",
    "                sum+=int(j)\n",
    "            if sum%2==0:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ret = 0\n",
    "        for i in range(1, num + 1):\n",
    "            s = str(i)\n",
    "            ret += sum(map(int, s)) & 1 == 0\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1, num + 1):\n",
    "            isOdd = True\n",
    "            cur = i\n",
    "            while cur > 0:\n",
    "                temp = cur % 10\n",
    "                cur = cur // 10\n",
    "                if temp % 2 == 1:\n",
    "                    isOdd = not isOdd\n",
    "            if isOdd:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        a=num % 10\n",
    "        k=num // 2\n",
    "        n=(a+(num // 10)+(num // 100)+(num // 1000)) % 2\n",
    "\n",
    "        if a % 2 == 1 or n == 0:\n",
    "            return k\n",
    "        return k - n\n",
    "           \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        answer = 0\n",
    "        for i in range(1,num+1):\n",
    "            sum0 = 0\n",
    "            for j in str(i):\n",
    "                sum0 += int(j)\n",
    "            if sum0 % 2 == 0:\n",
    "                answer += 1\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        x = 0\n",
    "        y = 1\n",
    "        for i in range(2,num+1):\n",
    "            if i % 10 == 0:\n",
    "                y ^= 1\n",
    "                if i%100 == 0:\n",
    "                    y ^= 1\n",
    "                    if i%1000 == 0:\n",
    "                        y ^= 1\n",
    "            if i+y&1:\n",
    "                x += 1\n",
    "        return x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for n in range(1, num + 1):\n",
    "            if sum(map(int, str(n))) % 2 == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        a,i=num % 10,num // 10\n",
    "        b,j=i % 10,i // 10\n",
    "        c,d=j % 10,j // 10\n",
    "        n = (a+b+c+d) % 2\n",
    "        if a % 2 == 1:\n",
    "            return num // 2\n",
    "        else :\n",
    "            return num // 2 - n\n",
    "           \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(1,num+1):\n",
    "            cur=sum([int(j) for j in str(i)])%2\n",
    "            if cur==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        return (num - 2) // 2 + 1 if sum(map(int, str(num))) % 2 == 0 else (num - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        return (num - (sum(map(int, str(num))) & 1)) >> 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(2,num+1):\n",
    "            s1=i//1000\n",
    "            s2=i//100\n",
    "            s3=i//10\n",
    "            s4=i%10\n",
    "            if (s1+s2+s3+s4)%2==0:\n",
    "                #只要四位相加为偶数即可,高位不管\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count = 0\n",
    "        for n in range(1, num+1):\n",
    "            if self.is_sum_odd(n):\n",
    "                count += 1\n",
    "        return count\n",
    "\n",
    "            \n",
    "\n",
    "    def is_sum_odd(self, n) -> bool:\n",
    "        sn = str(n)\n",
    "        total = 0\n",
    "        for ch in sn:\n",
    "            total += int(ch)\n",
    "        if total % 2==0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(2,num+1):\n",
    "            temp=0\n",
    "            while i>0:\n",
    "                temp+=i%10\n",
    "                i=i//10\n",
    "            if temp%2==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        if sum(map(int, str(num))) % 2 == 0:\n",
    "            return (num - 2) // 2 + 1\n",
    "        else:\n",
    "            return (num - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ret = 0\n",
    "        for i in range(num):\n",
    "            b = 0\n",
    "            a = list(map(int, str(i + 1)))\n",
    "            for j in range(len(a)):\n",
    "                b = b + a[j]\n",
    "            if b % 2 == 0:\n",
    "                ret = ret + 1\n",
    "            else:\n",
    "                continue\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans=(num//10)*5-1\n",
    "\n",
    "        x=num//10*10\n",
    "        #print(x)\n",
    "        for i in range(x,num+1):\n",
    "            tmp=sum([int(j) for j in str(i)])\n",
    "            if tmp%2==0:\n",
    "                #print(i)\n",
    "                ans+=1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ret = 0\n",
    "        for i in range(num):\n",
    "            b = 0\n",
    "            a = list(map(int, str(i + 1)))\n",
    "            for j in range(len(a)):\n",
    "                b = b + a[j]\n",
    "            if b % 2 == 0:\n",
    "                ret = ret + 1\n",
    "            else:\n",
    "                continue\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = num // 2\n",
    "        if num % 2 == 1:\n",
    "            return ans\n",
    "        t = 0\n",
    "        while num > 0:\n",
    "            t += num % 10\n",
    "            num //= 10\n",
    "        if t % 2 == 1:\n",
    "            ans -= 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        def allsum(n: int) -> bool:\n",
    "            #check all digit sum\n",
    "            s = 0\n",
    "            for i in str(n):\n",
    "                s += int(i)\n",
    "            return s % 2 == 0\n",
    "        \n",
    "        return sum([allsum(n) for n in range(1, num + 1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        # cnt=0\n",
    "        # for i in range(1,num+1):\n",
    "        #     if (i%10+i//10%10+i//100%10+i//1000%10)%2==0:\n",
    "        #         cnt+=1\n",
    "        # return cnt\n",
    "        if num%2==1:\n",
    "            return (num-1)//2\n",
    "        else:\n",
    "            if (num%10+num//10%10+num//100%10+num//1000%10)%2==0:\n",
    "                return (num-1)//2+1\n",
    "            else:\n",
    "                return (num-1)//2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        n=str(num)\n",
    "        s=0\n",
    "        for i in n:\n",
    "            s+=int(i)\n",
    "        if num%2==0 and s%2!=0:\n",
    "            return num // 2 - 1\n",
    "        else:\n",
    "            return num//2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        # cnt=0\n",
    "        # for i in range(1,num+1):\n",
    "        #     if (i%10+i//10%10+i//100%10+i//1000%10)%2==0:\n",
    "        #         cnt+=1\n",
    "        # return cnt\n",
    "        if num%2==1:\n",
    "            return (num-1)//2\n",
    "        else:\n",
    "            if (num%10+num//10%10+num//100%10+num//1000%10)%2==0:\n",
    "                return (num-1)//2+1\n",
    "            else:\n",
    "                return (num-1)//2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        a=num % 10\n",
    "        k=num // 2\n",
    "        n=(a+(num // 10)+(num // 100)+(num // 1000)) % 2\n",
    "\n",
    "        if a % 2 == 1 or n == 0:\n",
    "            return k\n",
    "        return k - n\n",
    "           \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        def check(num: int) -> bool:\n",
    "            return sum([int(i) for i in list(str(num))]) % 2 == 0\n",
    "        ans = 0\n",
    "        for i in range(1, num + 1):\n",
    "            if check(i):\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count = 0\n",
    "        for x in range(1,num+1):\n",
    "            s = 0\n",
    "            while x:\n",
    "                s += x % 10\n",
    "                x //= 10\n",
    "            if  s % 2 ==0:\n",
    "                count += 1\n",
    "        return count\n",
    "             \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(1,num+1):\n",
    "            if(i%2==0 and len(str(i))<2):\n",
    "                count+=1\n",
    "            elif(len(str(i))>1):\n",
    "                result=0\n",
    "                arr = list(str(i))\n",
    "                for i in arr:\n",
    "                    result+=int(i)\n",
    "                if(result%2==0):\n",
    "                    count+=1\n",
    "        return count\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for x in range(1, num+1):\n",
    "            if sum(int(c) for c in str(x)) % 2 == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for x in range(1,num+1):\n",
    "            s = 0\n",
    "            while x:\n",
    "                s += x % 10\n",
    "                x //= 10\n",
    "            ans += s % 2 == 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(2, num+1):\n",
    "            numls = list(str(i))\n",
    "            sumnum = 0\n",
    "            for j in numls:\n",
    "                sumnum += eval(j)\n",
    "            if sumnum % 2 == 0:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "   def countEven(self, num: int) -> int:\n",
    "        # 每十个数里就有5个和为偶数的数\n",
    "        # 0 ~ 9: 4\n",
    "        # 10 ~ 19: 5\n",
    "        return num//10 * 5 - 1 + ((num%10+1)//2 if sum(map(int,str(num//10))) % 2 else (num%10+2)//2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for x in range(1,num+1):\n",
    "            s=0\n",
    "            while x:\n",
    "                s+=x%10\n",
    "                x//=10\n",
    "            ans += s%2==0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        for x in range(1, num + 1):\n",
    "            s = 0\n",
    "            while x:\n",
    "                s += x % 10\n",
    "                x //= 10\n",
    "            ans += s % 2 == 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        count=0\n",
    "        for i in range(2,num+1):\n",
    "            s1=i//1000\n",
    "            s2=i//100\n",
    "            s3=i//10\n",
    "            s4=i%10\n",
    "            if (s1+s2+s3+s4)%2==0:\n",
    "                #只要四位相加为偶数即可,高位不管\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        sum = 0\n",
    "        _sum = 0\n",
    "        for i in range(1, num + 1):\n",
    "            if i < 10:\n",
    "                if i % 2 == 0:\n",
    "                    sum += 1\n",
    "            else:\n",
    "                for _i in str(i):\n",
    "                    _sum += int(_i)\n",
    "                if _sum % 2 == 0:\n",
    "                    sum += 1\n",
    "                _sum = 0\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countEven(self, num: int) -> int:\n",
    "        y, x = divmod(num, 10)\n",
    "        ans = y * 5\n",
    "        y_sum = 0\n",
    "        while y:\n",
    "            y_sum += y % 10\n",
    "            y //= 10\n",
    "        return ans + ((x + 1) // 2 - 1 if y_sum % 2 else x // 2)\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
